﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using popLib;
using PhotoMgr.src.gui.contest;
using PhotoMgr.src.common;
using PhotoMgr.src.gui.category;
using PhotoMgr.src.gui.photo;

namespace PhotoMgr.src.gui
{
    public partial class MainForm : Form
    {
        private static MainForm mainform;

        public static MainForm Instance
        {
            get
            {
                if (mainform == null)
                    mainform = new MainForm();

                return mainform;
            }
        }

        public MainForm()
        {
            InitializeComponent();

            this.Icon = PhotoMgr.Properties.Resources.logo32;
        }

        public ReturnSet ShowExistedWindow(Type type)
        {
            foreach (var child in MdiChildren)
            {
                if (child.GetType().Equals(type))
                {
                    child.BringToFront();

                    if (child.WindowState == FormWindowState.Minimized)
                    {
                        child.WindowState = FormWindowState.Normal;
                    }
                    return new ReturnSet(true, child);
                }
            }
            return new ReturnSet(false);
        }

        public void AddMDIChild(Form form)
        {
            form.MdiParent = this;
            form.Show();
        }

        private bool CloseChildForms()
        {
            if (MdiChildren.Length > 0)
            {
                FormLib.alert("Xin vui lòng đóng tất cả các cửa sổ trước khi đăng xuất", "Thông báo");
                return false;
            }
            return true;
        }

        private void đăngXuấtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CloseChildForms())
                return;

            this.Hide();

            Login form = new Login();
            form.ShowDialog();

            if (form.DialogResult == System.Windows.Forms.DialogResult.OK)
            {
                //UpdateUserEnvironment();
                this.Show();
            }
            else
            {
                this.Close();
            }
        }

        private void quảnLýCuộcThiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ShowExistedWindow(typeof(ContestList)).Success)
                return;

            ContestList form = new ContestList("Quản lý cuộc thi",new FormAction()
            {
                AddRecord = true,
                ConfirmRecord = true,
                DisposeRecord = true,
                EditRecord = true,
                ViewRecord = true,
                SelectRecord = false
            });

            form.MdiParent = this;
            form.Show();
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ngườiDùngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ShowExistedWindow(typeof(UserList)).Success)
                return;

            UserList form = new UserList("Quản lý người dùng", new FormAction()
            {
                AddRecord = true,
                ConfirmRecord = true,
                DisposeRecord = true,
                EditRecord = true,
                ViewRecord = true,
                SelectRecord = false
            });

            form.MdiParent = this;
            form.Show();
        }

        private void quảnLýNghệSĩToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ShowExistedWindow(typeof(ArtistList)).Success)
                return;

            ArtistList form = new ArtistList("Quản lý tác giả", new FormAction()
            {
                AddRecord = true,
                ConfirmRecord = true,
                DisposeRecord = true,
                EditRecord = true,
                ViewRecord = true,
                SelectRecord = false
            });

            form.MdiParent = this;
            form.Show();
        }

        private void quảnLýChủĐềẢnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ShowExistedWindow(typeof(category.CategoryList)).Success)
                return;

            category.CategoryList form = new category.CategoryList("Quản lý chủ đề ảnh", new FormAction()
            {
                AddRecord = true,
                ConfirmRecord = true,
                DisposeRecord = true,
                EditRecord = true,
                ViewRecord = true,
                SelectRecord = false
            });

            form.MdiParent = this;
            form.Show();
        }

        private void quảnLýHìnhẢnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ShowExistedWindow(typeof(gui.Image.ImageList)).Success)
                return;

            gui.Image.ImageList form = new gui.Image.ImageList("Quản lý ảnh", new FormAction()
            {
                AddRecord = false,
                ConfirmRecord = false,
                DisposeRecord = false,
                EditRecord = false,
                ViewRecord = false,
                SelectRecord = false
            });

            form.MdiParent = this;
            form.Show();
        }

        private void thêmBộSưuTậpẢnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ShowExistedWindow(typeof(PhotoImport)).Success)
                return;

            PhotoImport form = new PhotoImport();
            form.MdiParent = this;
            form.Show();
        }

        private void quảnLýThiếtLậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ShowExistedWindow(typeof(SettingList)).Success)
                return;

            SettingList form = new SettingList("Quản lý thiết lập", new FormAction()
            {
                AddRecord = true,
                ConfirmRecord = true,
                DisposeRecord = true,
                EditRecord = true,
                ViewRecord = true,
                SelectRecord = false
            });

            form.MdiParent = this;
            form.Show();
        }

        private void MainForm_VisibleChanged(object sender, EventArgs e)
        {
            if ( this.Visible && UserAccount.Instance.RoleCode != 0)
            {
                FormLib.alert("Tên đăng nhập của bạn không có quyền sử dụng chức năng này!!!");

                đăngXuấtToolStripMenuItem_Click(null, new EventArgs());
            }
        }
    }
}
